# -*- coding: utf-8 -*-
# 1、用F12获取可以下载图片的url；
# 2、用parse.unquote()进行解码，然后将200换成0，即真实的高清壁纸；
# 3、url中的parge参数修改其值，来实现翻页，区间范围1-20页，区间0-19


from urllib import parse , request
import requests ,threading,queue
import os,time

headers = {
   'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
   'referer': 'https://pvp.qq.com/web201605/wallpaper.shtml'
    }
class producer(threading.Thread):
    def __init__(self,page_queue,image_queue,*args,**kwargs):
        super(producer, self).__init__(*args,**kwargs)
        self.page_queue = page_queue
        self.image_queue = image_queue
         
    def run(self,):
        while not self.page_queue.empty():
            page_url = self.page_queue.get()
            resp = requests.get(page_url,headers)
            result = resp.json()
            datas = result['List']
            for data in datas:
                image_urls = extract_image(data)
                name = parse.unquote(data['sProdName']).replace('1:1', '').strip()
                # images_1\猪八戒-年年有余
                dirpath = os.path.join('images_1',name)
                if not os.path.exists(dirpath):
                    os.mkdir(dirpath)
                for index,image_url in enumerate(image_urls):
                    self.image_queue.put({'image_url':image_url,'image_path':os.path.join(dirpath,'%d.jpg'%(index+1))})

class consumer(threading.Thread):
    def __init__(self,image_queue,*args,**kwargs):
        super(consumer, self).__init__(*args,**kwargs)
        self.image_queue = image_queue
        
    def run(self,):
        while True:
            try:
                image_obj = self.image_queue.get(timeout=10)
                image_url = image_obj.get('image_url')
                image_path = image_obj.get('image_path')
                try:
                    request.urlretrieve(image_url,image_path)
                    print(f'{image_path}已下载完成')
                except:
                    print(f'{image_path}下载失败！')
            except:
                break

def extract_image(data):
    image_urls = []
    for x in range(1,9):
        img_url =parse.unquote(data['sProdImgNo_%d'%x]).replace('200', '0')
        image_urls.append(img_url)
    return image_urls

def main():
    page_queue = queue.Queue(20)
    image_queue  = queue.Queue(1000)
    
    for x in range(0,20):
        page_url = 'https://apps.game.qq.com/cgi-bin/ams/module/ishow/V1.0/query/workList_inc.cgi?activityId=2735&sVerifyCode=ABCD&sDataType=JSON&iListNum=20&totalpage=0&page={page}&iOrder=0&iSortNumClose=1&iAMSActivityId=51991&_everyRead=true&iTypeId=2&iFlowId=267733&iActId=2735&iModuleId=2735&_=1620643784354'.format(page=x)
        page_queue.put(page_url)
        
    for x in range(3):
        th = producer(page_queue,image_queue,name=f'生产者{x}号')
        th.start()
    for x in range(6):  
        th = consumer(image_queue,name = f'消费者{x}号')
        th.start()
    
    print('已经全部下载完成！！')
            
    
if __name__ == '__main__':
    main()
    